import numpy as np
from scipy.spatial import ConvexHull
import matplotlib.pyplot as plt


def read_points_from_file(file_path):
    points = []
    with open(file_path, 'r') as file:
        for line in file:
            x, y = map(int, line.strip().split())
            points.append([x, y])
    return np.array(points)


def plot_points_and_hull(points, hull):
    plt.figure(figsize=(20, 16))
    plt.scatter(points[:, 0], points[:, 1], color='blue', label='Points')

    for i, txt in enumerate(points):
        plt.annotate(f"{int(txt[0])}, {int(txt[1])}", (points[i, 0], points[i, 1]), textcoords="offset points",
                     xytext=(0, 5), ha='center')

    for simplex in hull.simplices:
        plt.plot(points[simplex, 0], points[simplex, 1], 'r-')  # Convex hull lines in red

    # Mark convex hull points in red
    plt.scatter(points[hull.vertices, 0], points[hull.vertices, 1], color='red', label='Convex Hull Points')

    plt.title('Convex Hull')
    plt.xlabel('X-axis')
    plt.ylabel('Y-axis')
    plt.legend()
    plt.show()


if __name__ == '__main__':
    file_path = 'random_points_100.txt'
    points = read_points_from_file(file_path)

    hull = ConvexHull(points)

    print('Convex Hull Points:', points[hull.vertices])

    # Uncomment the following line to plot the points and convex hull
    plot_points_and_hull(points, hull)
